package com.wuzheng.wisgo.sanitation.cloud.paraandgetutil;

/**
 * 智慧环卫手动抛出异常
 * Created by sp on 2019/12/4.
 */
public class VehicleException extends Exception {


    private String className;
    private String errorMessage;
    private String methodName;
    private Object[] args;
    private Boolean systemError = false;


    /**
     * 获取系统异常实例。
     *
     * @param className
     * @param methodName
     * @param errorMessage
     * @param args
     * @return
     */
    public static VehicleException CreateSystemError(String className, String methodName, String errorMessage,
                                                     Object... args) {
        VehicleException result = new VehicleException(className, methodName, errorMessage, args);
        result.setSystemEror(true);
        return result;
    }


    /**
     * 获取系统异常实例。
     *
     * @param errorMessage
     * @param args
     * @return
     */
    public static VehicleException CreateSystemError(String errorMessage, Object... args) {
        VehicleException result = Exceptions.VehicleException(errorMessage, args);
        result.setSystemEror(true);
        return result;
    }


    /**
     * 获取非系统（业务）异常实例。
     *
     * @param className
     * @param methodName
     * @param errorMessage
     * @param args
     * @return
     */
    public static VehicleException CreateNoSystemError(String className, String methodName, String errorMessage,
                                                       Object... args) {
        VehicleException result = new VehicleException(className, methodName
                , errorMessage, args);
        result.setSystemEror(false);
        return result;
    }


    /**
     * 获取非系统（业务）异常实例。
     *
     * @param errorMessage
     * @param args
     * @return
     */
    public static VehicleException CreateNoSystemError(String errorMessage, Object... args) {
        VehicleException result = Exceptions.VehicleException(errorMessage, args);
        result.setSystemEror(false);
        return result;
    }


    public VehicleException(VehicleException ex) {
        this.errorMessage = ex.getMessage();
        this.methodName = ex.getMethodName();
        this.className = ex.getClassName();
        this.args = ex.getArgs();
        this.systemError = ex.getSystemError();
    }

    public VehicleException(String className, String methodName, String errorMessage, Object... args) {
        this.errorMessage = errorMessage;
        this.methodName = methodName;
        this.className = className;
        this.args = args;
    }


    public VehicleException(String className, String methodName, String errorMessage, Boolean systemEror) {
        this.errorMessage = errorMessage;
        this.methodName = methodName;
        this.className = className;
        this.systemError = systemEror;
    }


    public Boolean getSystemError() {
        return systemError;
    }

    public void setSystemEror(Boolean systemEror) {
        this.systemError = systemEror;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    /**
     * 获取异常信息
     *
     * @return
     */
    @Override
    public String getMessage() {
        return getFullMessage();
    }

    public String getFullMessage() {
        if (this.errorMessage == null || this.errorMessage.equals("")) {
            return super.getMessage();
        } else {
            return String.format(this.errorMessage, args);
        }
    }

    @Override
    public String toString() {
        return String.format("VehicleException:className=%s,methodName=%s,errorMessage=%s", className, methodName, String.format(errorMessage, this.args));
    }

    public Object[] getArgs() {
        return args;
    }

    public String getMethodName() {
        return methodName;
    }

}
